package repositories;

import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.util.ArrayList;

import specifications.AirlineSpecification;
import dbHelper.DBHelper;
import entities.Airline;

public class AirlineRepository implements Repository<Airline> {
	
	public ArrayList<Airline> AirlineList;
	
	
	public AirlineRepository()
	{
		this.AirlineList = null;
	}
	

	@Override
	public void Add(Airline Airline) {
		try {
			DBHelper.addObject(Airline);
		} catch (NoSuchMethodException | IllegalAccessException
				| IllegalArgumentException | InvocationTargetException
				| SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	@Override
	public void Delete(Airline Airline) {
		try {
			DBHelper.deleteObject(Airline);
		} catch (IllegalAccessException | IllegalArgumentException
				| InvocationTargetException | NoSuchMethodException
				| SecurityException | SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	@Override
	public void Update(Airline Airline) {
		try {
			DBHelper.updateObject(Airline);
		} catch (NoSuchMethodException | IllegalAccessException
				| IllegalArgumentException | InvocationTargetException
				| SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	@Override
	public void FindAll() {
		Airline A = new Airline();
		try {
			this.AirlineList = (ArrayList<Airline>) DBHelper.findObjects(A);
		} catch (IllegalAccessException | IllegalArgumentException
				| InvocationTargetException | InstantiationException
				| NoSuchMethodException | SecurityException | SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	public ArrayList<Airline> findBySpecification (AirlineSpecification spec) {
		if(this.AirlineList == null)
			this.FindAll();
		ArrayList<Airline> res = new ArrayList<Airline>();
		for (Airline el : AirlineList)
		{
			if (spec.isSatisfiedBy(el))
			{
				res.add(el);
			}
		}
		return res;
	}

}
